﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace MartinRL.ProjectEuler.Sequences
{
    public class FibonacciSequence : IEnumerable<int>
    {
        private readonly IEnumerable<int> _sequence;

        private FibonacciSequence()
        {
            _sequence = 0.ToMax().Select(t => t.CalculateFibonacciTerm());
        }

        public static IEnumerable<int> NewSequence()
        {
            return new FibonacciSequence();
        }

        public IEnumerator<int> GetEnumerator()
        {
            return _sequence.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }
    }

    public static class FibonacciSequence_Extensions
    {
        public static int CalculateFibonacciTerm(this int @this)
        {
            if (@this == 0)
                return 0;

            if (@this == 1)
                return 1;

            var term = CalculateFibonacciTerm(@this - 1) + CalculateFibonacciTerm(@this - 2);

            return term;
        }
    }
}